import {constantRouterIcon} from './router-icons';
import {RouteRecordRaw} from 'vue-router';
import {Layout, ParentLayout} from '@/router/constant';
import type {AppRouteRecordRaw} from '@/router/types';

// const Iframe = () => import('@/views/iframe/index.vue');
const LayoutMap = new Map<string, () => Promise<typeof import('*.vue')>>();

LayoutMap.set('LAYOUT', Layout);
// LayoutMap.set('IFRAME', Iframe);

export const routerGenerator = (routerMap, parent?): any[] => {
    return routerMap.map((item) => {
        const currentRouter: any = {
            path: `${(parent && parent.path) || ''}/${item.path}`,
            name: item.name || '',
            component: item.component,
            meta: {
                ...item.meta,
                label: item.meta.title,
                icon: constantRouterIcon[item.meta.icon] || null,
                permissions: item.meta.permissions || null,
            },
        };
        currentRouter.path = currentRouter.path.replace('//', '/');
        item.redirect && (currentRouter.redirect = item.redirect);
        if (item.children && item.children.length > 0) {
            !item.redirect && (currentRouter.redirect = `${item.path}/${item.children[0].path}`);
            currentRouter.children = routerGenerator(item.children, currentRouter);
        }
        return currentRouter;
    });
};

/**
 * 动态生成菜单
 */
export const generatorDynamicRouter = (): Promise<RouteRecordRaw[]> => {
    return new Promise((resolve,) => {
        // adminMenus()
        //     .then((result) => {
                const routeList = routerGenerator([]);
                asyncImportRoute(routeList);
                resolve(routeList);
            // })
            // .catch((err) => {
            //     reject(err);
            // });
    });
};

/**
 * 查找views中对应的组件文件
 * */
let viewsModules: Record<string, () => Promise<Recordable>>;
export const asyncImportRoute = (routes: AppRouteRecordRaw[] | undefined): void => {
    viewsModules = viewsModules || import.meta.glob('../views/**/*.{vue,tsx}');
    if (!routes) return;
    routes.forEach((item) => {
        if (!item.component && item.meta?.frameSrc) {
            item.component = 'IFRAME';
        }
        const {component, name} = item;
        const {children} = item;
        if (component) {
            const layoutFound = LayoutMap.get(component as string);
            if (layoutFound) {
                item.component = layoutFound;
            } else {
                item.component = dynamicImport(viewsModules, component as string);
            }
        } else if (name) {
            item.component = ParentLayout;
        }
        children && asyncImportRoute(children);
    });
};

/**
 * 动态导入
 * */
export const dynamicImport = (
    viewsModules: Record<string, () => Promise<Recordable>>,
    component: string
) => {
    const keys = Object.keys(viewsModules);
    const matchKeys = keys.filter((key) => {
        let k = key.replace('../views', '');
        const lastIndex = k.lastIndexOf('.');
        k = k.substring(0, lastIndex);
        return k === component;
    });
    if (matchKeys?.length === 1) {
        const matchKey = matchKeys[0];
        return viewsModules[matchKey];
    }
    if (matchKeys?.length > 1) {
        console.warn(
            'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure'
        );
        return;
    }
};
